﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml.Linq;

namespace ueEngine.Framework.Configuration
{
    public class SiteResource
    {
        #region 站点配置
        static Resource resource = null;
        static SiteResource()
        {
            XElement xe = XElement.Load(HttpContext.Current.Server.MapPath("/content/config/sitesetting.xml"));
            resource = new Resource()
            {
                Host = new Resource_Host()
                {
                    Domain = xe.Element("host").Element("domain").Value,
                    SiteName = xe.Element("host").Element("name").Value,
                    Company = xe.Element("host").Element("company").Elements()
                        .ToDictionary(
                            xn => { return xn.Name.ToString(); },
                            xn => { return xn.Value; })
                },
                Page = new Resource_Page()
                {
                    Default = xe.Element("page").Elements("default")
                        .ToDictionary(
                            xn => { return xn.Attribute("group").Value; },
                            xn =>
                            {
                                return xn.Attributes().Where(c => c.Name.ToString() != "group")
                                    .ToDictionary(
                                        xa => { return xa.Name.ToString(); },
                                        xa => { return xa.Value; }
                                    );
                            }
                         ),
                    Routing = xe.Element("page").Elements("item")
                        .ToDictionary(
                            xn => { return xn.Attribute("name").Value; },
                            xn =>
                            {
                                return xn.Attributes().Where(c => c.Name.ToString() != "name")
                                    .ToDictionary(
                                        xa => { return xa.Name.ToString(); },
                                        xa => { return xa.Value; }
                                    );
                            })
                },
                AdminAccount = new Resource_Admin()
                {
                    AdminAccount = xe.Element("admin").Element("account").Elements("item")
                        .Select(c => c.Attribute("value").Value)
                        .ToList()
                }
            };
        }

        public static string Domain
        {
            get
            {
                return resource.Host.Domain;
            }
        }
        public static string SiteName
        {
            get
            {
                return resource.Host.SiteName;
            }
        }
        public static string CompanyAddress
        {
            get
            {
                return resource.Host.Company["address"];
            }
        }
        public static string CompanyPhone
        {
            get
            {
                return resource.Host.Company["phone"];
            }
        }
        public static List<string> AdminAccount
        {
            get
            {
                return resource.AdminAccount.AdminAccount;
            }
        }
        public static string Title(string defaultgroup = "default")
        {
            string uri = HttpContext.Current.Request.Path.ToLower();
            if (resource.Page.Routing.Keys.Contains(uri))
            {
                Dictionary<string,string> routing = resource.Page.Routing[uri];
                if (routing.ContainsKey("title"))
                {
                    string title = routing["title"];
                    if (!String.IsNullOrEmpty(title))
                    {
                        return title;
                    }
                }
            }
            return resource.Page.Default[defaultgroup]["title"];
        }
        public static string Description(string defaultgroup = "default")
        {
            string uri = HttpContext.Current.Request.Path.ToLower();
            if (resource.Page.Routing.Keys.Contains(uri))
            {
                Dictionary<string, string> routing = resource.Page.Routing[uri];
                if (routing.ContainsKey("description"))
                {
                    string description = routing["description"];
                    if (!String.IsNullOrEmpty(description))
                    {
                        return description;
                    }
                }
            }
            return resource.Page.Default[defaultgroup]["description"];

        }
        public static string KeyWord(string defaultgroup = "default")
        {
            string uri = HttpContext.Current.Request.Path.ToLower();
            if (resource.Page.Routing.Keys.Contains(uri))
            {
                Dictionary<string, string> routing = resource.Page.Routing[uri];
                if (routing.ContainsKey("description"))
                {
                    string keyword = routing["keyword"];
                    if (!String.IsNullOrEmpty(keyword))
                    {
                        return keyword;
                    }
                }
            }
            return resource.Page.Default[defaultgroup]["keyword"];
        }

        public class Resource
        {
            public Resource_Host Host { get; set; }
            public Resource_Page Page { get; set; }
            public Resource_Admin AdminAccount { get; set; }
        }
        public class Resource_Host
        {
            /// <summary>
            /// 站点域名
            /// </summary>
            public string Domain { get; set; }
            /// <summary>
            /// 站点名
            /// </summary>
            public string SiteName { get; set; }
            public Dictionary<string, string> Company { get; set; }
        }
        public class Resource_Page
        {
            public Dictionary<string,Dictionary<string, string>> Default { get; set; }
            public Dictionary<string, Dictionary<string, string>> Routing { get; set; }
        }
        public class Resource_Admin
        {
            public List<string> AdminAccount { get; set; }
        }
        #endregion

        #region 
        public static bool IsAdmin()
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                return AdminAccount.Any(c => c == HttpContext.Current.User.Identity.Name);
            }
            return false;
        }
        #endregion
    }
}
